<template>
  <el-container class="frame" style="height: 100%">
    <!-- 侧边栏 -->
    <el-aside :width="isMobile? '0' : (isCollapse? '64px' : '240px')" class="my-aside">
      <div class="aside-header">
        <img src="@/assets/images/scau-logo.png" alt="Logo" class="logo-img" />
        <h5 v-if="!isCollapse && !isMobile" class="logo">SCAU - 物业管理系统</h5>
      </div>
      <!-- 滚动容器 -->
      <div class="aside-scroll">
        <el-menu
            :collapse="isCollapse"
            class="menu-style"
            default-active="statistics-users"
            @select="pageChange"
        >
          <!-- 数据统计 -->
          <el-sub-menu index="statistics" class="side-menu-item">
            <template #title>
              <span style="color: white">数据统计</span>
            </template>
            <el-menu-item class="side-menu-item" index="statistics-users">用户数据统计</el-menu-item>
            <el-menu-item class="side-menu-item" index="statistics-complaints">小区投诉与建议数据统计</el-menu-item>
            <el-menu-item class="side-menu-item" index="statistics-parking">小区车位使用数据统计</el-menu-item>
          </el-sub-menu>
          <!-- 管理功能 -->
          <el-sub-menu index="management" class="side-menu-item">
            <template #title>
              <span style="color: white">管理功能</span>
            </template>
            <el-menu-item class="side-menu-item" index="user-statistics">用户管理</el-menu-item>
            <el-menu-item class="side-menu-item" index="community-info-management">小区管理</el-menu-item>
            <el-menu-item class="side-menu-item" index="building-info-management">楼栋管理</el-menu-item>
            <el-menu-item class="side-menu-item" index="house-info-management">房屋管理</el-menu-item>
            <el-menu-item class="side-menu-item" index="charge-detail-management">缴费详情管理</el-menu-item>
            <el-menu-item class="side-menu-item" index="charge-item-management">缴费项目管理</el-menu-item>
            <el-menu-item class="side-menu-item" index="complaint-suggestion-stats">投诉与建议管理</el-menu-item>
            <el-menu-item class="side-menu-item" index="notice-statistics">通知管理</el-menu-item>
            <el-menu-item class="side-menu-item" index="repair-management">报修管理</el-menu-item>
            <el-menu-item class="side-menu-item" index="parking-management">车位管理</el-menu-item>
            <el-menu-item class="side-menu-item" index="parking-use-detail">车位使用管理</el-menu-item>
            <el-menu-item class="side-menu-item" index="user-activity-log">日志管理</el-menu-item>
          </el-sub-menu>
        </el-menu>
      </div>
    </el-aside>
    <el-container style="width: 100%; height: 100%">
      <!-- 顶部栏 -->
      <el-header class="my-top-nav" :style="{ left: headerLeft }">
        <div style="display: flex; align-items: center;">
          <el-button text @click="toggleMenu">
            <el-icon>
              <component :is="isCollapse? Expand : Fold" />
            </el-icon>
            <h5 style="margin-left: 10px">管理员端</h5>
          </el-button>
        </div>
        <div class="user-info">
          <span style="margin-right: 10px;">欢迎你, 管理员</span>
          <el-button type="info" round @click="handleLogout">退出登录</el-button>
        </div>
      </el-header>
      <!-- 主体内容 -->
      <el-main class="main-content" :style="{ marginLeft: mainContentMarginLeft }">
        <router-view class="content-area" />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { ref, computed, onMounted } from 'vue';
import { Menu, Fold, Expand } from '@element-plus/icons-vue';
import { useRouter } from 'vue-router';

export default {
  computed: {
    Fold() {
      return Fold;
    },
    Expand() {
      return Expand;
    },
    // 新增计算属性，计算主体内容区域的margin-left值
    mainContentMarginLeft() {
      if (this.isMobile) {
        return '0px';
      }
      return this.isCollapse? '64px' : '240px';
    }
  },
  components: {
    [Menu.name]: Menu,
    [Fold.name]: Fold,
    [Expand.name]: Expand
  },
  setup() {
    const isCollapse = ref(false);
    const isMobile = ref(false);
    const router = useRouter();

    const checkScreen = () => {
      isMobile.value = window.innerWidth < 768;
    };

    const toggleMenu = () => {
      isCollapse.value = !isCollapse.value;
    };

    const headerLeft = computed(() => {
      if (isMobile.value) return '0px';
      return isCollapse.value? '64px' : '240px';
    });

    onMounted(() => {
      checkScreen();
      window.addEventListener('resize', checkScreen);
    });

    return {
      isCollapse,
      isMobile,
      toggleMenu,
      headerLeft,
      pageChange(url) {
        router.push(url);
      },
      handleLogout() {
        localStorage.clear();
        router.push('/');
      }
    };
  }
};
</script>

<style scoped>
.logo {
  color: white;
  text-align: center;
}

.side-menu-item {
  background-color: #304055;
  color: white; /* 设置字体颜色为白色 */
}

/* 白色箭头 */
::v-deep(.el-sub-menu__icon-arrow) {
  color: white!important;
}

::v-deep(.el-sub-menu__title:hover),
::v-deep(.el-menu-item:hover) {
  background-color: black!important;
  color: white!important;
}

.my-aside {
  height: 100vh;
  background-color: #304055;
  overflow: hidden; /* 防止整体侧边栏多余部分显示 */
  transition: width 0.3s;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
  display: flex;
  flex-direction: column;
}

.aside-header {
  height: 50px;
  display: flex;
  align-items: center;
  padding: 0 10px;
  flex-shrink: 0;
  background-color: #2d3747;
}

/* 滚动容器，让菜单可以滚动 */
.aside-scroll {
  flex: 1;
  overflow-y: auto;
}

/* 可选：自定义滚动条样式 */
.aside-scroll::-webkit-scrollbar {
  width: 4px;
}

.aside-scroll::-webkit-scrollbar-thumb {
  background-color: rgba(255, 255, 255, 0.3);
  border-radius: 4px;
}

.logo-img {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: white;
  margin-right: 10px;
}

.my-top-nav {
  position: fixed;
  top: 0;
  right: 0;
  height: 50px;
  padding: 0 20px;
  background-color: white;
  border-bottom: 1px solid #e5e4e4;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 1000;
  transition: left 0.3s ease;
}

.main-content {
  padding: 0;
  margin-top: 30px;
  /* 移除固定的margin-left值 */
  transition: margin-left 0.3s; /* 可添加过渡效果，使布局变化更流畅 */
  height: auto; /* 根据内容自动调整高度 */
  overflow: hidden; /* 禁止它滚动 */
  display: flex;
  flex-direction: column;
}

.content-area {
  background-color: #f2f5fa;
  padding: 0;
  height: auto; /* 根据内容自动调整高度 */
  overflow-y: auto; /* 启用滚动条（仅在内容超出时显示） */
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); /* 添加阴影效果 */
}

/* 响应式适配 */
@media (max-width: 768px) {
  .main-content,
  .my-aside {
    width: 0!important;
  }
  .logo {
    display: none;
  }
}
</style>